/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef OHMW_FRAMEWORK_CXX_MESSAGE_TIME_H
#define OHMW_FRAMEWORK_CXX_MESSAGE_TIME_H

#include <cstdint>
#include <ostream>
#include <vector>

namespace OHOS {
namespace OHMW {
class Time {
public:
    Time() : secs(0), nano_secs(0) {}
    Time(uint32_t secs, uint32_t nano_secs) : secs(secs), nano_secs(nano_secs) {}
    Time(const Time&) = default;
    Time& operator=(const Time&) = default;
    ~Time() = default;

    template<typename E>
    void EncodeUnchecked(E& encoder, std::vector<uint8_t>& buffer) const {
        encoder.PutUnchecked(buffer, secs);
        encoder.PutUnchecked(buffer, nano_secs);
    }
    
    template<typename E>
    bool Encode(E& encoder, std::vector<uint8_t>& buffer) const {
        return encoder.Put(buffer, secs) && encoder.Put(buffer, nano_secs);
    }

    template<typename D>
    static void DecodeUnchecked(D& decoder, const std::vector<uint8_t>& buffer, Time& time) {
        decoder.GetUnchecked(buffer, time.secs);
        decoder.GetUnchecked(buffer, time.nano_secs);
    }

    template<typename D>
    static bool Decode(D& decoder, const std::vector<uint8_t>& buffer, Time& time) {
        if (!decoder.Get(buffer, time.secs) || !decoder.Get(buffer, time.nano_secs)) {
            return false;
        }
        return true;
    }

    std::ostream& ToRon(std::ostream& os) const {
        os << "(secs:" << secs << ",nano_secs:" << nano_secs << ")";
        return os;
    }

    uint32_t secs;
    uint32_t nano_secs;
};
}
}

#endif
